package com.nbjtjc.safe.service.emg.handle;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.handle.EmgHandleListMapper;
import com.nbjtjc.safe.model.emg.handle.EmgHandleList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class EmgHandleListService {
    @Autowired
    private EmgHandleListMapper emgHandleListMapper;

    public Result find(EmgHandleList emgHandleList){
        Result result=new Result();
        PageHelper.startPage(emgHandleList.getPageCurrent(),emgHandleList.getPageSize());
        List<EmgHandleList> emgHandleLists=emgHandleListMapper.search(emgHandleList);
        if(CollectionUtil.isEmpty(emgHandleLists)){
            result.setData(new PageInfo<>(new ArrayList<>()));
        }else{
            result.setData(new PageInfo<>(emgHandleLists));
        }
        return result;
    }
    @Transactional(rollbackFor = {Throwable.class})
    public Result add(EmgHandleList emgHandleList){
        Result result=new Result();
        if(ObjectUtil.isNull(emgHandleList)
                ||ObjectUtil.isNull(emgHandleList.getHandleId())){
            result.setFalse("主要内容不能呢个为空");
            return result;
        }
        int res=0;
        if(ObjectUtil.isNull(emgHandleList.getId())){
            res=emgHandleListMapper.insertSelective(emgHandleList);
        }else{
            res=emgHandleListMapper.updateByPrimaryKeySelective(emgHandleList);
        }
        if(res==0){
            result.setFalse("插入或者更新失败");
            return result;
        }
        result.setData(emgHandleList);
        return result;
    }
    @Transactional(rollbackFor = {Throwable.class})
    public Result delete(Integer id){
        Result result=new Result();
        if(ObjectUtil.isNull(id)){
            result.setFalse("id不能为空");
            return result;
        }
        int res=emgHandleListMapper.deleteByPrimaryKey(id);
        return result;
    }

}
